using System;
using System.Collections.Generic;
using System.Configuration;

namespace Tyng.MediaWiki
{
    internal static class Config
    {
        public static Configuration.ApiSleepSettingsCollection Sleep
        {
            get
            {
                Configuration.ApiSleepSection section = (Configuration.ApiSleepSection)ConfigurationManager.GetSection("mediaWikiSleep");
                if (section == null) return null;
                return section.ApiSleep;
            }
        }
    }


    namespace Configuration
    {

        [ConfigurationCollection(typeof(ApiSleepSettings))]
        public sealed class ApiSleepSettingsCollection : ConfigurationElementCollection
        {
            // Fields
            private static ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();

            // Methods
            public ApiSleepSettingsCollection()
                : base(StringComparer.OrdinalIgnoreCase)
            {
            }

            public void Add(ApiSleepSettings settings)
            {
                this.BaseAdd(settings);
            }

            protected override void BaseAdd(int index, ConfigurationElement element)
            {
                if (index == -1)
                {
                    base.BaseAdd(element, false);
                }
                else
                {
                    base.BaseAdd(index, element);
                }
            }

            public void Clear()
            {
                base.BaseClear();
            }

            protected override ConfigurationElement CreateNewElement()
            {
                return new ApiSleepSettings();
            }

            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((ApiSleepSettings)element).Key;
            }

            public void Remove(ApiSleepSettings settings)
            {
                if (base.BaseIndexOf(settings) >= 0)
                {
                    base.BaseRemove(settings.Key);
                }
            }

            public void Remove(string name)
            {
                base.BaseRemove(name);
            }

            public void RemoveAt(int index)
            {
                base.BaseRemoveAt(index);
            }

            public ApiSleepSettings this[int index]
            {
                get
                {
                    return (ApiSleepSettings)base.BaseGet(index);
                }
                set
                {
                    if (base.BaseGet(index) != null)
                    {
                        base.BaseRemoveAt(index);
                    }
                    this.BaseAdd(index, value);
                }
            }

            public bool ContainsKey(string name)
            {
                return this.BaseGet(name) != null;
            }

            public int GetSleep(string name)
            {
                return ((ApiSleepSettings)this.BaseGet(name)).Sleep;
            }

            protected override ConfigurationPropertyCollection Properties
            {
                get
                {
                    return _properties;
                }
            }
        }

        public sealed class ApiSleepSettings : ConfigurationElement
        {
            private static readonly ConfigurationProperty _propName = new ConfigurationProperty("name", typeof(string), null, null, new StringValidator(1), ConfigurationPropertyOptions.IsKey | ConfigurationPropertyOptions.IsRequired);
            private static readonly ConfigurationProperty _propSleep = new ConfigurationProperty("sleep", typeof(int), null, null, new IntegerValidator(0, int.MaxValue), ConfigurationPropertyOptions.IsRequired);
            private static ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();


            // Methods
            static ApiSleepSettings()
            {
                _properties.Add(_propName);
                _properties.Add(_propSleep);
            }

            public ApiSleepSettings()
            {
            }

            public ApiSleepSettings(string name, int sleep)
            {
                this.Name = name;
                this.Sleep = sleep;
            }

            internal string Key
            {
                get
                {
                    return this.Name;
                }
            }

            [ConfigurationProperty("name", Options = ConfigurationPropertyOptions.IsKey | ConfigurationPropertyOptions.IsRequired, DefaultValue = "")]
            public string Name
            {
                get
                {
                    return (string)base[_propName];
                }
                set
                {
                    base[_propName] = value;
                }
            }

            [ConfigurationProperty("sleep", Options = ConfigurationPropertyOptions.IsRequired, DefaultValue = "0")]
            public int Sleep
            {
                get
                {
                    return (int)base[_propSleep];
                }
                set
                {
                    base[_propSleep] = value;
                }
            }

            protected override ConfigurationPropertyCollection Properties
            {
                get
                {
                    return _properties;
                }
            }
        }

        public sealed class ApiSleepSection : ConfigurationSection
        {
            // Fields
            private static readonly ConfigurationProperty _propApiSleep = new ConfigurationProperty(null, typeof(ApiSleepSettingsCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
            private static ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();

            // Methods
            static ApiSleepSection()
            {

                _properties.Add(_propApiSleep);
            }

            protected override void InitializeDefault()
            {
                base.InitializeDefault();

                ApiSleep.Add(new ApiSleepSettings("query", 1000));
                ApiSleep.Add(new ApiSleepSettings("edit", 60000));
                ApiSleep.Add(new ApiSleepSettings("login", 60000));
            }

            protected override object GetRuntimeObject()
            {
                this.SetReadOnly();
                return this;
            }

            // Properties
            [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
            public ApiSleepSettingsCollection ApiSleep
            {
                get
                {
                    return (ApiSleepSettingsCollection)base[_propApiSleep];
                }
            }

            protected override ConfigurationPropertyCollection Properties
            {
                get
                {
                    return _properties;
                }
            }
        }



    }

}